<?php
	require(__DATAGEN_CLASSES__ . '/TableProjectBidGen.class.php');

	/**
	 * The TableProjectBid class defined here contains any
	 * customized code for the TableProjectBid class in the
	 * Object Relational Model.  It represents the "table_project_bid" table 
	 * in the database, and extends from the code generated abstract TableProjectBidGen
	 * class, which contains all the basic CRUD-type functionality as well as
	 * basic methods to handle relationships and index-based loading.
	 * 
	 * @package My Application
	 * @subpackage DataObjects
	 * 
	 */
	class TableProjectBid extends TableProjectBidGen {
		/**
		 * Default "to string" handler
		 * Allows pages to _p()/echo()/print() this object, and to define the default
		 * way this object would be outputted.
		 *
		 * Can also be called directly via $objTableProjectBid->__toString().
		 *
		 * @return string a nicely formatted string representation of this object
		 */	
		public function __toString() {
			return sprintf('%s',  $this->intId);
		}
		
	    //loders

        //properies
        public static function bool_bidding_for_the_project($user_id_,$project_id_)
        {
            $objDatabase = TableProjectBid::GetDatabase();

            //$strParam1 = $objDatabase->SqlVariable($strParam1);

            $strQuery = 'SELECT count(*) FROM `table_project_bid` AS `table_project_bid` WHERE project_id='.$project_id_.' and user_id='.$user_id_;

            //$strQuery = 'SELECT * FROM table_site WHERE name like `a%`';

            $objDbResult = $objDatabase->Query($strQuery);
            $objResult=$objDbResult->FetchArray();
            if($objResult[0]>0)return true;

            return false;
        }
		
		  public static function bool_bid_id_placed_for_the_project($user_id_,$project_id_,$bid_id)
        {
            $objDatabase = TableProjectBid::GetDatabase();

            //$strParam1 = $objDatabase->SqlVariable($strParam1);

          $strQuery = 'SELECT id FROM `table_project_bid` AS `table_project_bid` WHERE project_id='.$project_id_.' and buyer_id='.$user_id_.' and id='.$bid_id;

            //$strQuery = 'SELECT * FROM table_site WHERE name like `a%`';

             $objDbResult = $objDatabase->Query($strQuery);
            $objResult=$objDbResult->FetchArray();
            if($objResult[0]>0)return true;

       

            return false;
        }
        
          public static function bid_id_for_the_project_for_buyer($buyer_id_,$project_id_)
        {
            $objDatabase = TableProjectBid::GetDatabase();

            //$strParam1 = $objDatabase->SqlVariable($strParam1);

          $strQuery1 = 'SELECT id FROM `table_project_bid` AS `table_project_bid` WHERE project_id='.$project_id_.' and buyer_id='.$buyer_id_.'';

            //$strQuery = 'SELECT * FROM table_site WHERE name like `a%`';

            $objDbResult = $objDatabase->Query($strQuery1);
            $objResult=$objDbResult->FetchArray();
            if($objResult[0]>0)return $objResult['id'];

            return false;
        }
        
         public static function load_users_by_project_bid($project_id_)
        {
            $objDatabase = TableProjectBid::GetDatabase();

            //$strParam1 = $objDatabase->SqlVariable($strParam1);

          $strQuery = 'SELECT buyers_id FROM `table_project_bid` AS `table_project_bid` WHERE project_id='.$project_id_.' and status=0';

            //$strQuery = 'SELECT * FROM table_site WHERE name like `a%`';

             $objDbResult = $objDatabase->Query($strQuery);
        
             return tableprojectbid::InstantiateDbResult($objDbResult);
        }
        
        public static function LoadArrayByBuyerIdProjectIdStatus($intBuyerId, $intProjectId, $intStatus, $objOptionalClauses = null) {
			// Call TableProjectBid::QueryArray to perform the LoadArrayByUserIdProjectIdStatus query
			try {
				return TableProjectBid::QueryArray(
					QQ::AndCondition(
					QQ::Equal(QQN::TableProjectBid()->BuyerId, $intBuyerId),
					QQ::Equal(QQN::TableProjectBid()->ProjectId, $intProjectId),
					QQ::Equal(QQN::TableProjectBid()->Status, $intStatus)
					),
					$objOptionalClauses);
			} catch (QCallerException $objExc) {
				$objExc->IncrementOffset();
				throw $objExc;
			}
		}
		
		
		 public function bool_my_bid($key_)
        {
            if($this->UserId==$key_)
            {
                return true;
            }
            return false;
        }

        public function bool_bid_for_the_project($key_)
        {
            if($this->ProjectId==$key_)
            {
                return true;
            }
            return false;
        }

        public function bool_bid_for_my_project($key_)
        {
            if($this->BuyerId==$key_)
            {
                return true;
            }
            return false;
        }

        //created & active
        public function bool_active()
        {
            if($this->Status==0)
            {
                return true;
            }
            return false;
        }

        //retracted
        public function bool_inactive()
        {
            if($this->Status==1)
            {
                return true;
            }
            return false;
        }

        //won
        public function bool_won()
        {
            if($this->Status==2)
            {
                return true;
            }
            return false;
        }

        //timeout or not selected
        public function bool_not_selected()
        {
            if($this->Status==3)
            {
                return true;
            }
            return false;
        }
        
          public function bool_cancelled()
        {
            if($this->Status==7)
            {
                return true;
            }
            return false;
        }

        //get

        //textlink
        public function get_textlink_to_select_bid_popup()
        {
            return '<a target="blank" href="'.$this->get_href_to_select_bid_popup().'">'.'[select provider]'.'</a>';
        }

        public function get_textlink_to_cancel_bid_popup()
        {
            return '<a target="blank" href="'.$this->get_href_to_cancel_bid_popup().'">'.'[cancel bid]'.'</a>';
        }

        public function get_textlink_to_edit_bid_popup()
        {
            return '<a target="blank" href="'.$this->get_href_to_edit_bid_popup().'">'.'[edit bid]'.'</a>';
        }
         public function get_textlink_to_retract_bid_popup()
        {
            return '<a target="blank" href="'.$this->get_href_to_retract_bid_popup().'">'.'&nbsp;&nbsp;retract bid&nbsp;&nbsp;'.'<img src="media/popup_small.gif" alt="" border="0" align="absmiddle" /></a>';
        }

        //href
        public function get_href_to_select_bid_popup()
        {
            return '/popup_select_bid.php?id='.$this->Project->Id.'&bid='.$this->Id;
        }

        public function get_href_to_cancel_bid_popup()
        {
            return '/popup_cancel_bid.php?id='.$this->Project->Id.'&bid='.$this->Id;
        }

        public function get_href_to_edit_bid_popup()
        {
            return '/popup_edit_bid.php?id='.$this->Project->Id.'&bid='.$this->Id;
        }
         public function get_href_to_retract_bid_popup()
        {
            return '/popup_retract_bid.php?id='.$this->Project->Id.'&bid='.$this->Id;
        }

        //set
        //active
        public function set_status_active()
        {
            $this->Status=0;
        }

        //retracted
        public function set_status_inactive()
        {
            $this->Status=1;
        }

        //won
        public function set_status_won()
        {
            $this->Status=2;
        }

        //timeout or not selected
        public function set_status_not_selected()
        {
            $this->Status=3;
        }
        
          //cancelled by user
        public function set_status_cancelled()
        {
            $this->Status=7;
        }

        //functions
        public function set_bid_date_for_now()
        {
            $this->BidDate=QDateTime::Now();
        }

        public function set_retract_date_for_now()
        {
            $this->RetractDate=QDateTime::Now();
        }

        //update
        public static function update_bids_info_for_the_project($id_)
        {
            $project_=TableProject::LoadById($id_);

            if(isset($project_))
            {
                $objDatabase = TableProject::GetDatabase();

                $strParam1 = $objDatabase->SqlVariable($strParam1);

                $strQuery = 'SELECT avg(bid) FROM `table_bid` AS `table_bid` WHERE project_id='.$id_;

                $objDbResult = $objDatabase->Query($strQuery);
                $objResult=$objDbResult->FetchArray();

            }
        }
        
        public function load_buyer_all_for_the_project_order_by_bid_date($objOptionalClauses = null) {

            $objDatabase = TableProjectBid::GetDatabase();

            //$strParam1 = $objDatabase->SqlVariable($strParam1);

            $strQuery = sprintf('SELECT `table_project_bid`.* FROM `table_project_bid` AS `table_project_bid` WHERE project_id=%s and status!=1 order by bid_date',$this->Id);

            $objDbResult = $objDatabase->Query($strQuery);
            //$objResult=$objDbResult->FetchArray();
            //return $objResult[0];
            return TableProjectBid::InstantiateDbResult($objDbResult);
        }


		// Override or Create New Load/Count methods
		// (For obvious reasons, these methods are commented out...
		// but feel free to use these as a starting point)
/*
		public static function LoadArrayBySample($strParam1, $intParam2, $objOptionalClauses = null) {
			// This will return an array of TableProjectBid objects
			return TableProjectBid::QueryArray(
				QQ::AndCondition(
					QQ::Equal(QQN::TableProjectBid()->Param1, $strParam1),
					QQ::GreaterThan(QQN::TableProjectBid()->Param2, $intParam2)
				),
				$objOptionalClauses
			);
		}

		public static function LoadBySample($strParam1, $intParam2, $objOptionalClauses = null) {
			// This will return a single TableProjectBid object
			return TableProjectBid::QuerySingle(
				QQ::AndCondition(
					QQ::Equal(QQN::TableProjectBid()->Param1, $strParam1),
					QQ::GreaterThan(QQN::TableProjectBid()->Param2, $intParam2)
				),
				$objOptionalClauses
			);
		}

		public static function CountBySample($strParam1, $intParam2, $objOptionalClauses = null) {
			// This will return a count of TableProjectBid objects
			return TableProjectBid::QueryCount(
				QQ::AndCondition(
					QQ::Equal(QQN::TableProjectBid()->Param1, $strParam1),
					QQ::Equal(QQN::TableProjectBid()->Param2, $intParam2)
				),
				$objOptionalClauses
			);
		}

		public static function LoadArrayBySample($strParam1, $intParam2, $objOptionalClauses) {
			// Performing the load manually (instead of using QCubed Query)

			// Get the Database Object for this Class
			$objDatabase = TableProjectBid::GetDatabase();

			// Properly Escape All Input Parameters using Database->SqlVariable()
			$strParam1 = $objDatabase->SqlVariable($strParam1);
			$intParam2 = $objDatabase->SqlVariable($intParam2);

			// Setup the SQL Query
			$strQuery = sprintf('
				SELECT
					`table_project_bid`.*
				FROM
					`table_project_bid` AS `table_project_bid`
				WHERE
					param_1 = %s AND
					param_2 < %s',
				$strParam1, $intParam2);

			// Perform the Query and Instantiate the Result
			$objDbResult = $objDatabase->Query($strQuery);
			return TableProjectBid::InstantiateDbResult($objDbResult);
		}
*/




		// Override or Create New Properties and Variables
		// For performance reasons, these variables and __set and __get override methods
		// are commented out.  But if you wish to implement or override any
		// of the data generated properties, please feel free to uncomment them.
/*
		protected $strSomeNewProperty;

		public function __get($strName) {
			switch ($strName) {
				case 'SomeNewProperty': return $this->strSomeNewProperty;

				default:
					try {
						return parent::__get($strName);
					} catch (QCallerException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}
			}
		}

		public function __set($strName, $mixValue) {
			switch ($strName) {
				case 'SomeNewProperty':
					try {
						return ($this->strSomeNewProperty = QType::Cast($mixValue, QType::String));
					} catch (QInvalidCastException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}

				default:
					try {
						return (parent::__set($strName, $mixValue));
					} catch (QCallerException $objExc) {
						$objExc->IncrementOffset();
						throw $objExc;
					}
			}
		}
*/
	}
?>